[_ISTool]
EnableISX=true

[Setup]
AppName=GaduBot
AppVerName=GaduBot 1.0.0.6
;MinVersion=4.1,4.0
DefaultDirName={pf}\GaduBot
DefaultGroupName=GaduBot
UninstallDisplayIcon={app}\GaduBot.exe
Compression=lzma
SolidCompression=true
OutputBaseFilename=setup-1-0-0-6
OutputDir=.\bin\Setup
LicenseFile=GNU GPL License.txt
PrivilegesRequired=admin
WizardSmallImageFile=compiler:WizModernSmallImage-IS.bmp
WizardImageFile=compiler:WizModernImage-IS.bmp
DisableProgramGroupPage=no
AllowRootDirectory=yes


[Run]
Filename: {app}\GaduBot.exe; WorkingDir: {app}; Description: Launch application; Flags: postinstall nowait skipifsilent unchecked

[Files]
Source: C:\Program Files\ISTool\isxdl.dll; Flags: dontcopy
Source: bin\Release\*.*; DestDir: {app}; Flags: recursesubdirs createallsubdirs

[Messages]
WinVersionTooLowError=GaduBot requires Windows NT4, Windows 98 or later.

[Icons]
Name: {group}\GaduBot; Filename: {app}\GaduBot.exe; WorkingDir: {app}; Tasks: startmenu
Name: {group}\Uninstall GaduBot; Filename: {uninstallexe}; Tasks: startmenu
Name: {commonstartup}\GaduBot; Filename: {app}\GaduBot.exe; WorkingDir: {app}
Name: {commondesktop}\GaduBot; Filename: {app}\GaduBot.exe; WorkingDir: {app}; Tasks: desktopicon\common
Name: {userdesktop}\GaduBot; Filename: {app}\GaduBot.exe; WorkingDir: {app}; Tasks: desktopicon\user

;[Registry]
;Root: HKLM; Subkey: "Software\GaduBot"; ValueType: string; ValueName: "AppPath"; ValueData: "{app}"\

[Tasks]
Name: startmenu; Description: Create icon in Menu Start; GroupDescription: Start Menu:
Name: desktopicon; Description: Create a &desktop icon; GroupDescription: Additional icons:
Name: desktopicon\common; Description: For all users; GroupDescription: Additional icons:; Flags: exclusive
Name: desktopicon\user; Description: For the current user only; GroupDescription: Additional icons:; Flags: exclusive unchecked
Name: quicklaunchicon; Description: Create a &Quick Launch icon; GroupDescription: Additional icons:; Flags: unchecked


[Code]
var
  dotnetRedistPath: string;
  downloadNeeded: boolean;
  dotNetNeeded: boolean;
  memoDependenciesNeeded: string;

procedure isxdl_AddFile(URL, Filename: PChar);
external 'isxdl_AddFile@files:isxdl.dll stdcall';
function isxdl_DownloadFiles(hWnd: Integer): Integer;
external 'isxdl_DownloadFiles@files:isxdl.dll stdcall';
function isxdl_SetOption(Option, Value: PChar): Integer;
external 'isxdl_SetOption@files:isxdl.dll stdcall';


const
  dotnetRedistURL = 'http://download.microsoft.com/download/6/0/f/60fc5854-3cb8-4892-b6db-bd4f42510f28/dotnetfx35.exe';
  // local system for testing...
  // dotnetRedistURL = 'http://192.168.1.1/dotnetfx.exe';

function InitializeSetup(): Boolean;

begin
  Result := true;
  dotNetNeeded := false;

  // Check for required netfx installation
  if (not RegKeyExists(HKLM, 'Software\Microsoft\.NETFramework\policy\v1.1')) then begin
    dotNetNeeded := true;
    if (not IsAdminLoggedOn()) then begin
      MsgBox('MyApp needs the Microsoft .NET Framework to be installed by an Administrator', mbInformation, MB_OK);
      Result := false;
    end else begin
      memoDependenciesNeeded := memoDependenciesNeeded + '      .NET Framework' #13;
      dotnetRedistPath := ExpandConstant('{src}\dotnetfx.exe');
      if not FileExists(dotnetRedistPath) then begin
        dotnetRedistPath := ExpandConstant('{tmp}\dotnetfx.exe');
        if not FileExists(dotnetRedistPath) then begin
          isxdl_AddFile(dotnetRedistURL, dotnetRedistPath);
          downloadNeeded := true;
        end;
      end;
      SetIniString('install', 'dotnetRedist', dotnetRedistPath, ExpandConstant('{tmp}\dep.ini'));
    end;
  end;

end;

function NextButtonClick(CurPage: Integer): Boolean;
var
  hWnd: Integer;
  ResultCode: Integer;

begin
  Result := true;

  if CurPage = wpReady then begin

    hWnd := StrToInt(ExpandConstant('{wizardhwnd}'));

    // don't try to init isxdl if it's not needed because it will error on < ie 3
    if downloadNeeded then begin

      isxdl_SetOption('label', 'Downloading Microsoft .NET Framework');
      isxdl_SetOption('description', 'MyApp needs to install the Microsoft .NET Framework. Please wait while Setup is downloading extra files to your computer.');
      if isxdl_DownloadFiles(hWnd) = 0 then Result := false;
    end;
    if (Result = true) and (dotNetNeeded = true) then begin
      if Exec(ExpandConstant(dotnetRedistPath), '', '', SW_SHOW, ewWaitUntilTerminated, ResultCode) then begin
         // handle success if necessary; ResultCode contains the exit code
         if not (ResultCode = 0) then begin
           Result := false;
         end;
      end else begin
         // handle failure if necessary; ResultCode contains the error code
         Result := false;
      end;
    end;
  end;
end;

function UpdateReadyMemo(Space, NewLine, MemoUserInfoInfo, MemoDirInfo, MemoTypeInfo, MemoComponentsInfo, MemoGroupInfo, MemoTasksInfo: String): String;
var
  s: string;

begin
  if memoDependenciesNeeded <> '' then s := s + 'Dependencies to install:' + NewLine + memoDependenciesNeeded + NewLine;
  s := s + MemoDirInfo + NewLine + NewLine;

  Result := s
end;
